package ast;

import compiler.OutputFormatter;

public class Function {
	
	private Type type;
	private String name;
	private VarDecList vdl;
	private StatementList stl;
	private ArgList pList;
	
	public Function(Type type, String name, ArgList pList) {
		this.setType(type);
		this.setName(name);
		this.setpList(pList);
	}

	public void setType(Type type) {
		this.type = type;
	}

	public Type getType() {
		return type;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setVdl(VarDecList vdl) {
		this.vdl = vdl;
	}

	public VarDecList getVdl() {
		return vdl;
	}

	public void setStl(StatementList stl) {
		this.stl = stl;
	}

	public StatementList getStl() {
		return stl;
	}

	public void setpList(ArgList pList) {
		this.pList = pList;
	}

	public ArgList getpList() {
		return pList;
	}
	
	/**
	 * Gets the number of arguments of this function
	 * @return the number of arguments of this function
	 */
	public int getNumberOfArguments() {
		return pList.getSize(); 
	}
	
	/**
	 * Returns the argument type of a given position
	 * @param argPos the position of the argument
	 * @return the type of the argument at the given position
	 */
	public Type getArgType(int argPos) {
		return pList.getArgType(argPos);
	}
	
	/**
	 * Generates the C source code of the current knot
	 * @param of The OutputFormatter where the code will be generated
	 */
	public void genC(OutputFormatter of) {
		if(type != null)
			type.genC(of);
		else
			of.output("void");
		of.output(" " + name + " (");
		pList.genC(of);
		of.output(") {");
		of.endLine();
		of.increaseIndent();
		vdl.genC(of);
		stl.genC(of);
		of.decreaseIndent();
		of.output("}");
		of.endLine();
	}
	
}
